import json

import requests

from category.videogeneratorbase import GenerateVideoBase
from config import *


class NewsGenerateVideo(GenerateVideoBase):
    """
    数据案例
    [
        {'page_no': 0,
         'model': {'date': "3月20日", "stock_code": "600418", "stock_name": "江淮汽车", "desc": "江淮汽车（600418）盘中涨停"},
         'audio_text': '三月二十日，江淮汽车盘中涨停。'},
        {'page_no': 1,
         'model': {'desc': "《亚盛医药白血病新药提交上市申请》", "img_url": "http://127.0.0.1:8083/file/5efb014190085cba28723edd.png"},
         'audio_text': '异动原因：《亚盛医药白血病新药提交上市申请》'},
        {'page_no': 2,
         'model': {'desc': "《关于江淮汽车股票交易异常波动的回函》确认要进入战略投资者确认要进入战略投资者确",
                   "img_url": "http://127.0.0.1:8083/file/5efb015e90085cba28723ee0.png"},
         'audio_text': '关于江淮汽车股票交易异常波动的回函,确认要进入战略投资者确认要进入战略投资者确'},
        {'page_no': 3,
         'model': {'desc': "江淮汽车战略投资者回顾江淮汽车战略投资者回顾江淮投资者回顾江淮江淮投资者回顾江淮",
                   "img_url": "http://127.0.0.1:8083/file/5efb02b490085cba28723ee4.png"},
         'audio_text': '公司已于近期与CDE进行了新药上市申请提交前 的沟通交流，申请依据HQP1351的两项关键性注册临床研究结果提交NDA。CDE同意公司提交该药物针对这两个适应症的NDA。'},
    ]
    """
    api_host = None

    def __init__(self, res_id, ppt_tpl_path, api_host):
        GenerateVideoBase.__init__(self, res_id, ppt_tpl_path)
        self.api_host = api_host

    def fetch_script_data(self):
        # 12
        response = requests.get(url=self.api_host + "/v1/pic/get_news_script/" + str(self.res_id))
        res = json.loads(response.text)
        # print(res)
        self.script_data = res

    # 同步生成数据至服务器
    def sync_result_to_server(self):
        video_path = self.generate_result['video_path']
        print('1@'+video_path + '@' + self.res_id)
        # video_screenshot_path = self.generate_result['video_screenshot_path']
        duration = 0 # self.generate_result['duration']
        url = 'http://videostocks.films.cn/api/dfs/upload'
        with open(video_path, 'rb') as vf:
            response = requests.post(url, files={'file': vf})
            print('2@'+response.text)
            res = json.loads(response.text)
            # print(res)
            if res['resultCode'] != 0:
                print("upload video error {}".format(res))
                return
            
            video_url = res['data']['url'].replace('/','_')
            print(video_url)
            requests.get('http://jfj8.bjyihuilian.com/v1/pic/edit_news_scripts_video_url/'+self.res_id+'/'+video_url)
            requests.get('http://jfj8.bjyihuilian.com/v1/pic/edit_news_scripts_status/'+self.res_id+'/8')

            print("upload video file success res_id {}".format(self.res_id))

    def handle_spec_slide(self, page_no, slide, model):
        # 如果是第二页
        if page_no == 1:
            self.replace_image(slide, slide.shapes[4], model['img_url'])
        elif page_no == 2:
            self.replace_image(slide, slide.shapes[3], model['img_url'])
        elif page_no == 3:
            self.replace_image(slide, slide.shapes[0], model['img_url'])


if __name__ == '__main__':
    res_id = '5f0530fcac31b0c7a1001178'
    generator = NewsGenerateVideo(res_id=res_id, ppt_tpl_path=TEMPLATE_PATH + 'spec_news_stock.pptx',
                                  api_host="http://videostocks.films.cn")
    generator.fetch_script_data()
    generator.generate()


'''

'''
